/*
 * @description:
 * @Author: lal
 * @Date: 2021-07-09 09:16:42
 * @LastEditors: lal
 * @LastEditTime: 2021-07-09 10:24:06
 */
/*
 * @lc app=leetcode.cn id=3 lang=javascript
 *
 * [3] 无重复字符的最长子串
 */

// @lc code=start
/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function (s) {
  // 3.无重复字符的最长子串
  let j = 0 // 定义左指针
  let maxLength = 0 // 结果
  let set = new Set() // 存放字符和对应下标
  for (let i = 0; i < s.length; i++) {
    if (!set.has(s[i])) {
      set.add(s[i])
      maxLength = Math.max(maxLength, set.size)
    } else {
      while (set.has(s[i])) {
        set.delete(s[j])
        j++
      }
      set.add(s[i])
    }
  }
  return maxLength
}

// @lc code=end
